<template>
    <transition>
        <el-dialog :title="dialogType === 'add' ? '添加考试安排' : '修改考试安排'" v-model="showFlag" width="600px" append-to-body
            @close="handleClose" @open="handleOpen">
            <el-form hide-required-asterisk ref="formRef" :rules="formRule" :model="formInfo" label-width="auto"
                style="padding:30px 30px 0 30px">
                <el-form-item label="考试时间" prop="examTime">
                    <el-date-picker v-model="formInfo.examTime" type="datetimerange" range-separator="到"
                        start-placeholder="开始时间" end-placeholder="结束时间" value-format="YYYY-MM-DD hh:mm:ss" />
                </el-form-item>
                <el-form-item label="课程名称" prop="courseId">
                    <el-select placeholder="课程名称" v-model="formInfo.courseId">
                        <el-option v-for="e in courses" :key="e" :value="e.courseId"
                            :label="e.courseName"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="考试教室" prop="classroomId">
                    <el-select placeholder="考试教室" v-model="formInfo.classroomId">
                        <el-option v-for="e in classrooms" :key="e" :value="e.id" :label="e.name"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="考试班级" prop="classId">
                    <el-select placeholder="考试班级" v-model="formInfo.classId">
                        <el-option v-for="e in classes" :key="e" :value="e.id" :label="e.name"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="监考老师" prop="invigilatorId">
                    <el-select placeholder="监考老师" v-model="formInfo.invigilatorId">
                        <el-option v-for="e in teachers" :key="e" :value="e.id" :label="e.name"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="考试学期" prop="semesterId">
                    <el-select placeholder="考试学期" v-model="formInfo.semesterId">
                        <el-option v-for="e in semester" :key="e" :value="e.id" :label="e.name"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="考试学生人数" prop="studentCount">
                    <el-input placeholder="考试学生人数" type="number" v-model="formInfo.studentCount"></el-input>
                </el-form-item>
                <el-form-item style="margin-top: 40px; margin-left: 372px; ">
                    <el-button @click="handleClose" size="mini">取消</el-button>
                    <el-button @click="formSubmit" size="mini" type="primary">确定</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </transition>
</template>

<script setup>
import api from '@/api/api';
import { ElMessage } from 'element-plus';
import { onMounted, reactive, ref, watch } from 'vue';

const props = defineProps({
    dialogType: {
        type: String,
        default: 'add'
    }, showDialog: {
        type: Boolean
    },
    applyId: {
        default: ''
    },
    editForm: {
        type: Object
    }
})
const emit = defineEmits(['dialogClosed'])
const showFlag = ref(false)

const courses = ref([
    // courseId, courseName, className
])
const classes = ref([
    // id, name
])
const classrooms = ref([
    // id, name
])
const teachers = ref([
    // id, name
])
const semester = ref([
    // id, name
])

const formRef = ref(null)

const formRule = ref({
    examTime: [
        { required: true, message: '请填写考试时间', trigger: 'blur' }
    ],
    courseId: [
        { required: true, message: '请填写教材名称', trigger: 'blur' }
    ],
    classroomId: [
        { required: true, message: '请填写考试教室', trigger: 'blur' }
    ],
    classId: [
        { required: true, message: '请填写考试班级', trigger: 'blur' }
    ],
    invigilatorId: [
        { required: true, message: '请填写监考老师', trigger: 'blur' }
    ],
    semesterId: [
        { required: true, message: '请填写考试学期', trigger: 'blur' }
    ],
    studentCount: [
        { required: true, message: '请填写考试学生人数', trigger: 'blur' }
    ],
})
const formInfo = reactive(
    {
        'examTime': [], 'courseId': '', 'classId': '', 'classroomId': '', 'invigilatorId': '',
        'semesterId': '', 'studentCount': ''
    },
)
const formSubmit = () => {
    formRef.value.validate((valid) => {
        if (valid) {
            if (props.dialogType == 'add') {
                api.final_exam.add(
                    formInfo.examTime[0], formInfo.examTime[1], formInfo.courseId,
                    formInfo.classId,
                    formInfo.classroomId, formInfo.invigilatorId, formInfo.semesterId,
                    formInfo.studentCount
                ).then((res) => {
                    ElMessage({
                        message: '添加成功',
                        type: 'success'
                    })
                }).catch((err) => {
                    console.log(err)
                })

                handleClose()
            } else {
                api.final_exam.edit(
                    props.applyId, formInfo.examTime[0], formInfo.examTime[1], formInfo.courseId,
                    formInfo.classId,
                    formInfo.classroomId, formInfo.invigilatorId, formInfo.semesterId,
                    formInfo.studentCount
                ).then((res) => {
                    ElMessage({
                        message: '修改成功',
                        type: 'success'
                    })
                }).catch((err) => {
                    console.log(err)
                })

                handleClose()
            }

        }
    })
}
const handleClose = () => {
    showFlag.value = false
    formRef.value.resetFields()
    emit('dialogClosed')
}
const handleOpen = () => {
    if (props.dialogType === 'edit') {
        formInfo.courseId = props.editForm.courseId
        formInfo.classId = props.editForm.classId
        formInfo.classroomId = props.editForm.classroomId
        formInfo.invigilatorId = props.editForm.invigilatorId
        formInfo.semesterId = props.editForm.semesterId
        formInfo.studentCount = props.editForm.studentCount
    }
}

watch(() => props.showDialog, (newValue, oldValue) => {
    showFlag.value = props.showDialog
})


onMounted(() => {
    api.scheduled_course.get_all().then((res) => {
        let base_courses = res.data.data
        let course_set = new Set()
        for (let i = 0; i < base_courses.length; i++) {
            // 排课后一个course对应不同class，因此会有重复course
            // 所以需要筛掉重复的course
            if (!course_set.has(base_courses[i].courseId)) {
                course_set.add(base_courses[i].courseId)
                courses.value.push(base_courses[i])
            }
        }
    }).catch((err) => {
        console.log(err)
    })
    api.class_js.get_all().then((res) => {
        classes.value = res.data.data
    }).catch((err) => {
        console.log(err)
    })
    api.classroom.get_all().then((res) => {
        classrooms.value = res.data.data
    })
    api.semester.get_all().then((res) => {
        semester.value = res.data.data
    })
    api.teacher.get_all().then((res) => {
        teachers.value = res.data.data
    })
})
</script>